<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$tabs =array(
		array('op'=>'main','title'=>'传播数据'),
		array('op'=>'feedata','title'=>'转化数据'),
		array('op'=>'formdata','title'=>'表单数据'),
		array('op'=>'subdata','title'=>'留言数据'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='main'){
	$check=pdo_get('core_plugins',array('name'=>'jy_h5game'),'id');
	if(!$check){
		exi('还未安装“互动营销游戏”插件！请联系管理员！','error');
	}
	$count=array();
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$count['hexiao']=pdo_count('jy_h5game_hexiao',array('uniacid'=>$_SESSION['uniacid']));
	$count['award']=pdo_count('jy_h5game_award',array('uniacid'=>$_SESSION['uniacid']));
	$count['game']=pdo_count('jy_h5game_reply',array('uniacid'=>$_SESSION['uniacid']));
	$count['nowgame']=pdo_count('jy_h5game_reply',array('uniacid'=>$_SESSION['uniacid'],'endtime >'=>time()));
	$count['fans']=pdo_count('jy_h5game_fans',array('uniacid'=>$_SESSION['uniacid']));
	$time=mktime(0,0,0,date('m'));
	$count['newsfans']=pdo_count('jy_h5game_fans',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$time));
	$access['game']=pdo_count('core_access_log',array('this'=>2,'plugin'=>'jy_h5game'));
	$access['wxapp']=pdo_count('core_access_log',array('os'=>1,'plugin'=>'core','action'=>'welcome'));
	$access['xiu']=pdo_count('core_access_log',array('this'=>2,'plugin'=>'jy_xiu'));
	$access['h5web']=pdo_count('core_access_log',array('this'=>2,'plugin'=>'core','action'=>'welcome'));
	$access['all']=$access['game']+$access['wxapp']+$access['xiu']+$access['h5web'];
	//周份
	$work_weekarray=array('日','一','二','三','四','五','六');
	for($i=0;$i<7;$i++){
			if($i==0){
				$yuestart = $daytime-24*3600;
				$yueend = $daytime;
				
			}else{
				$yuestart = $daytime-$i*24*3600-24*3600;
				$yueend = $daytime-$i*24*3600;
			}
			$params=array('uniacid'=>$_SESSION['uniacid']);
			$params['createtime between']=array($yuestart,$yueend);
			$week = date('w',$yuestart);
			$weeks[] = $work_weekarray[$week];
			$weekaccess[]=pdo_count('core_access_log',$params);
			$weekfans[]=pdo_count('core_fans',$params,'id');
	}
	$weekaccess=array_reverse($weekaccess);
	$weekfans=array_reverse($weekfans);
	$weeks=array_reverse($weeks);
	//月份
	for($i=0;$i<12;$i++){
			if($i==0){
				$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
				$yueend = time();
				
			}else{
				$yuestart = mktime(0,0,0,date('m')-$i,1,date('Y'));
				$yueend = mktime(0,0,0,date('m')-$i+1,1,date('Y'));
			}
			$params=array('uniacid'=>$_SESSION['uniacid']);
			$params['createtime between']=array($yuestart,$yueend);
			$month12[] = date('Y-m',strtotime('-'.$i.' month'));
			$monthaccess[]=pdo_count('core_access_log',$params);
			$monthfans[]=pdo_count('core_fans',$params,'id');
	}
	$monthaccess=array_reverse($monthaccess);
	$monthfans=array_reverse($monthfans);
	$month12=array_reverse($month12);
	$return['htmls']=array(
					array('type'=>'data','one_num'=>2,'title'=>'传播流量数据','datas'=>array(
						array('title'=>'营销活动流量','data'=>$access['game']),
						array('title'=>'小程序宣传页流量','data'=>$access['wxapp']),
						array('title'=>'H5宣传页流量','data'=>$access['h5web']),
						array('title'=>'H5场景流量','data'=>$access['xiu']),
						array('title'=>'图片海报流量','data'=>$access['newsfans']),
						array('title'=>'活动表单流量','data'=>$access['fans']),
					)),
					array('type'=>'echarts_pie','one_num'=>2,'width'=>'50%','ecid'=>'wechat','title'=>'各类型宣传页营业额','datas'=>array(
						'time' => date("Y-m-d H:i:s", time()),
						'datas'=>array(
							array('name'=>'营销活动','value'=>$count['game']),
							array('name'=>'小程序宣传页','value'=>$count['game']),
							array('name'=>'H5宣传页','value'=>$count['hexiao']),
							array('name'=>'H5场景','value'=>$count['award']),
							array('name'=>'图片海报','value'=>$count['newsfans']),
							array('name'=>'活动表单','value'=>$count['fans']),
						),
					)),
					array('type'=>'echarts_pie','one_num'=>2,'width'=>'50%','ecid'=>'wxapp','title'=>'各类型宣传页转化率','datas'=>array(
						'time' => date("Y-m-d H:i:s", time()),
						'datas'=>array(
							array('name'=>'营销活动','value'=>22),
							array('name'=>'小程序宣传页','value'=>33),
							array('name'=>'H5宣传页','value'=>55),
							array('name'=>'H5场景','value'=>11),
							array('name'=>'图片海报','value'=>21),
							array('name'=>'活动表单','value'=>18),
						),
					)),
					/*array('type'=>'echarts_line','num'=>12,'ecid'=>'mobile','title'=>'周报数据指标','datas'=>array(
						'datas'=>array(
							array('name'=>'访问量','value'=>$weekaccess),
							array('name'=>'粉丝数','value'=>$weekfans),
						),
						'xdatas'=>$weeks,
					)),*/
					array('type'=>'echarts_arealine','num'=>12,'ecid'=>'mobile','title'=>'月度数据指标','datas'=>array(
						'datas'=>array(
							array('name'=>'访问量','value'=>$monthaccess),
							array('name'=>'粉丝数','value'=>$monthfans),
						),
						'xdatas'=>$month12,
					)),
	);
	exi($return);
}
if($_RQ['op']=='subdata'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>1);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d',$item['createtime']);
			$item['title']=pdo_getcolumn('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['aid']),'title');
			$item['title'] = $item['title'] ? $item['title'] : '非文章留言';
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
					array('op'=>'main','title'=>'传播数据'),
					array('op'=>'feedata','title'=>'转化数据'),
					array('op'=>'formdata','title'=>'表单数据'),
					array('op'=>'subdata','title'=>'留言数据'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'username','title'=>'姓名','type'=>'string'),
			array('name'=>'userphone','title'=>'电话','type'=>'string'),
			array('name'=>'content','title'=>'留言内容','type'=>'string'),
			array('name'=>'title','title'=>'所属文章','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'username','help'=>'客户姓名','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='feedata'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>1);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=array();
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d',$item['createtime']);
			$item['title']=pdo_getcolumn('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['aid']),'title');
			$item['title'] = $item['title'] ? $item['title'] : '非文章留言';
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'main','title'=>'传播数据'),
			array('op'=>'feedata','title'=>'转化数据'),
			array('op'=>'formdata','title'=>'表单数据'),
			array('op'=>'subdata','title'=>'留言数据'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'用户ID','type'=>'string'),
			array('name'=>'username','title'=>'用户昵称','type'=>'string'),
			array('name'=>'xiutitle','title'=>'溯源宣传页','type'=>'string'),
			array('name'=>'type','title'=>'溯源宣传类型','type'=>'string'),
			array('name'=>'fee','title'=>'成交金额','type'=>'string'),
			array('name'=>'createtime','title'=>'成交时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'username','help'=>'宣传页名称','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'留言',
		'tabs'=>array(
			array('op'=>'list','title'=>'留言列表'),
			array('op'=>'formlist','title'=>'自定义表单列表'),
		),
		'edit'=>array(
			array('type'=>'copy','title'=>'留言姓名','name'=>'username', 'help'=>'' ),
			array('type'=>'copy','title'=>'电话','name'=>'userphone', 'help'=>'' ),
			array('type'=>'copy','title'=>'邮箱','name'=>'useremail', 'help'=>'' ),
			array('type'=>'copy','title'=>'内容','name'=>'content', 'help'=>'' ),
			array('type'=>'copy','title'=>'留言时间','name'=>'createtime', 'help'=>'' ),
			array('type'=>'switch','title'=>'状态','name'=>'status', 'help'=>'' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'status'=>trim($postdata['status']),
		'content'=>trim($postdata['content'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_message',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('留言修改成功！');
	}else{
		exi('没有找到相关留言');
	}
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('core_account_message',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='formdata'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=1;
	if($_RQ['mid']){
        $params['uid']=intval($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
			$item['avatar'] = $member['avatar'];
			$item['nickname'] = $member['nickname'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
					array('op'=>'main','title'=>'传播数据'),
					array('op'=>'feedata','title'=>'转化数据'),
					array('op'=>'formdata','title'=>'表单数据'),
					array('op'=>'subdata','title'=>'留言数据'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'页面','type'=>'string'),
			array('name'=>'nickname','title'=>'客户名称','type'=>'string'),
			array('name'=>'avatar','title'=>'客户头像','type'=>'image'),
			array('name'=>'createtime','title'=>'提交时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'表单详情'),
			)
		),
		'search'=>array(
            array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
            array('name'=>'title','width'=>15,'title'=>'页面名称','type'=>'string'),
        )
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择留言','error');
	}
	
	$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$item['avatar'] = $member['avatar'];
	$item['nickname'] = $member['nickname'];
	$item['data']=$item['data']?json_decode($item['data'],true):array();
	$formtypes=get_form_types();
	
	foreach($item['data'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	//print_r($item);exit;
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'desc'=>'留言信息',
		'look'=>array(
				array('type'=>'string','title'=>'用户名称','name'=>'nickname','help'=>''),
				array('type'=>'image','title'=>'用户头像','name'=>'avatar','help'=>''),
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'data',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=1;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mid']){
        $params['uid']=intval($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
	if(!empty($_RQ['pageaction'])){
		$params['pageaction']=trim($_RQ['pageaction']);
		$return['params']['pageaction']=$_RQ['pageaction'];
	}
    //创建时间
    if($_RQ['createtime']){
        $time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
        $time_range=json_decode($time_range,true);
        $params['createtime between']=array($time_range['tstart'],$time_range['tend']);
        $return['params']['createtime']=$time_range;
    }
    $header=array(
        'id'=>'ID',
		'title'=>'页面',
        'nickname'=>'昵称',
        'mobile'=>'电话',
    );
	$order=pdo_getall('core_account_message',$params,'','','id desc');
	foreach($order as &$item){
		$item['createtime'] = date('Y-m-d H:i:s',$item['createtime']);
		$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('mobile','nickname'));
		$item['mobile'] = $member['mobile'];
		$item['nickname'] = $member['nickname'];
		$item['data']=$item['data']?json_decode($item['data'],true):array();
		$formtypes=get_form_types();
		$item['data2']='';
		if($item['posttype']==1){
			foreach($item['data'] as $dk=>&$diyv){
				$dk +=1;
				foreach($diyv as $k=>&$v){
					$item['data2'] .=$v['title']."：";
					if(is_array($v['data']) && $v['type']!=7){
						$v['data']=$v['data'][0].' '.$v['data'][1].' '.$v['data'][2].' '.$v['data'][3];
					}
					if($v['type']==7){
						$v['data']=$v['data']['province'].' '.$v['data']['city'].' '.$v['data']['district'];
					}
					$item['data2'] .=$v['data'];
					//$v['type']=$formtypes[$v['type']];
                    $header['from'.$k]=$v['title'];
                    $item['from'.$k]=$v['data'];
				}
				//$item['data2'] .="\n";
			}
		}else{
			foreach($item['data'] as $k=>&$v){
				$item['data2'] .=$v['title']."：";
				if(is_array($v['data']) && $v['type']!=7){
						$v['data']=$v['data'][0].' '.$v['data'][1].' '.$v['data'][2].' '.$v['data'][3];
				}
				if($v['type']==7){
						$v['data']=$v['data']['province'].' '.$v['data']['city'].' '.$v['data']['district'];
				}
				$item['data2'] .=$v['data'];
                $header['from'.$k]=$v['title'];
                $item['from'.$k]=$v['data'];
				//$item['data2'] .="\n";
			}
		}
	}
    $header['data2']='汇总内容';
    $header['createtime']='时间';
	cfc('tools')->export2excel($header,$order,'表单');
}